package view;

import module.SrtHandle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import app.SrtFix;

public class EditManualDialog extends Dialog {

	protected Object result;
	protected Shell shell;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public EditManualDialog(Shell parent) {
		super(parent, SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);

	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		int width = 200;
		int height = 180;
		shell.setSize(width, height);
		shell.setText("\u624B\u52A8\u4FEE\u6B63");
		Point winPoint = SrtFix.app.shell.getLocation();
		Point winSize = SrtFix.app.shell.getSize();
		shell.setLocation(winPoint.x + (winSize.x - width) / 2, winPoint.y + (winSize.y - height) / 2);
		shell.setLayout(new RowLayout(SWT.HORIZONTAL));

		final Button c_empty_row = new Button(shell, SWT.CHECK);
		c_empty_row.setLayoutData(new RowData(180, SWT.DEFAULT));
		c_empty_row.setSelection(true);
		c_empty_row.setText("\u5220\u9664\u7A7A\u884C");

		final Button c_remove_eff = new Button(shell, SWT.CHECK);
		c_remove_eff.setLayoutData(new RowData(180, SWT.DEFAULT));
		c_remove_eff.setSelection(true);
		c_remove_eff.setText("\u5220\u9664ass\u7279\u6548");
		
		final Button c_del_eng = new Button(shell, SWT.CHECK);
		c_del_eng.setLayoutData(new RowData(180, SWT.DEFAULT));
		c_del_eng.setSelection(true);
		c_del_eng.setText("\u5220\u9664\u82F1\u6587");

		final Button c_sort_time = new Button(shell, SWT.CHECK);
		c_sort_time.setLayoutData(new RowData(180, SWT.DEFAULT));
		c_sort_time.setSelection(true);
		c_sort_time.setText("\u6309\u65F6\u95F4\u6392\u5E8F");

		final Button c_rebuild_id = new Button(shell, SWT.CHECK);
		c_rebuild_id.setLayoutData(new RowData(180, SWT.DEFAULT));
		c_rebuild_id.setSelection(true);
		c_rebuild_id.setText("\u4FEE\u6B63\u7F16\u53F7");

		final Combo combo = new Combo(shell, SWT.NONE);
		combo.setLayoutData(new RowData(75, SWT.DEFAULT));
		combo.setItems(new String[] { "GBK\u7F16\u7801", "UTF-8\u7F16\u7801", "Unicode\u7F16\u7801" });
		if (SrtFix.app.tableControl.srtfile.code == SrtFix.app.tableControl.srtfile.unicode)
			combo.select(2);
		else if (SrtFix.app.tableControl.srtfile.code == SrtFix.app.tableControl.srtfile.utf8)
			combo.select(1);
		else
			combo.select(0);
		Button btn_fix = new Button(shell, SWT.NONE);
		btn_fix.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int fix = 0;
				if (c_empty_row.getSelection())
					fix = fix | SrtHandle.removeEmptyRow;
				if (c_remove_eff.getSelection())
					fix = fix | SrtHandle.removeEff;
				if (c_sort_time.getSelection())
					fix = fix | SrtHandle.sorTime;
				if (c_rebuild_id.getSelection())
					fix = fix | SrtHandle.rebuildId;
				if (c_del_eng.getSelection())
					fix = fix | SrtHandle.delEng;
				switch (combo.getSelectionIndex()) {
				case 2:
					SrtFix.app.tableControl.srtfile.code = SrtFix.app.tableControl.srtfile.unicode;
					break;
				case 1:
					SrtFix.app.tableControl.srtfile.code = SrtFix.app.tableControl.srtfile.utf8;
					break;
				default:
					SrtFix.app.tableControl.srtfile.code = SrtFix.app.tableControl.srtfile.gbk;
					break;
				}
				SrtFix.app.tableControl.manualFix(fix);
				shell.dispose();
			}
		});
		btn_fix.setText("\u4FEE\u6B63");

		Button btn_close = new Button(shell, SWT.NONE);
		btn_close.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.dispose();
			}
		});
		btn_close.setText("\u53D6\u6D88");
	}

}
